<?php
Yii::import('application.models.geo.GeoActiveRecord');

/**
 * This is the model class for table "district_has_public_location".
 *
 * The followings are the available columns in table 'district_has_public_location':
 * @property string $district_id
 * @property string $public_location_id
 * @property string $city_id
 *
 * The followings are the available model relations:
 * @property City $city
 * @property PublicLocation[] $publicLocations
 */
class District extends GeoActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return District the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'district';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('district_id, public_location_id, city_id', 'required'),
			array('district_id, public_location_id, city_id', 'length', 'max'=>10),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('district_id, public_location_id, city_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'city' => array(self::BELONGS_TO, 'City', 'city_id'),
			'publicLocations' => array(self::MANY_MANY, 'PublicLocation', 'district_has_public_location(district_id, public_location_id)'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'district_id' => 'District',
			'public_location_id' => 'Public Location',
			'city_id' => 'City',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('district_id',$this->district_id,true);
		$criteria->compare('public_location_id',$this->public_location_id,true);
		$criteria->compare('city_id',$this->city_id,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
    
    public function getDataByCity($city_alias, $key = 'alias'){
        if(is_numeric($city_alias)){
            $city_alias = City::model()->getAliasById($city_alias);
        }
        return CHtml::listData(City::model()->dataAll[$city_alias]['districts'], $key, 'name');
    }

    public function getDataRefByKey($key = 'id'){ // id, alias
        $data = array();
        foreach(City::model()->dataAll as $city){
            foreach($city['districts'] as $district){
                $data[$district[$key]] = $district;    
            }   
        }
        return $data;
    }
    
    public function getDataGroupByCity(){
        $data = array();
        foreach(City::model()->dataAll as $city){
            foreach($city['districts'] as $district){
                $data[$city['id']][$district['id']] = $district['name'];     
            }
        }
        return $data;
    }
}